﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace OpenRPOSBackOffice
{
    public partial class OptionGroupSelect : ChildWindow
    {
        public Options options;

        public OptionGroupSelect()
        {
            InitializeComponent();
        }

        public void init()
        {
            foreach (ServiceReferenceOpenRPOS.OptionGroup og in Globals.db.OptionGroupRecords)
            {
                cmbOptionGroups.Items.Add(og.col_Name);
            }
            if (cmbOptionGroups.Items.Count() > 0)
                cmbOptionGroups.SelectedIndex = 0;
        }

        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            // Add the selected option group
            if (cmbOptionGroups.SelectedIndex >= 0)
            {
                if(options.lbOptionGroups.Items.Contains(cmbOptionGroups.SelectedValue.ToString())){
                    MessageBox.Show("Option group has already been added.");
                    return;
                }
                ServiceReferenceOpenRPOS.MenuItemOptionGroup miog = new ServiceReferenceOpenRPOS.MenuItemOptionGroup();
                miog.col_OptionGroupID = Globals.db.OptionGroupRecords[cmbOptionGroups.SelectedIndex].col_ID;
                miog.bFilterOptionGroupID = true;
                miog.col_MenuItemID = options.menuItemID;
                miog.bFilterMenuItemID = true;
                miog.col_Precedence = (short)(options.max_precedence + 1);
                miog.bFilterPrecedence = true;
                await Globals.db.AddMenuItemOptionGroupObject(miog);
                await Globals.db.GetMenuItemOptionGroupObjects("MenuItemID=" + options.menuItemID + " order by Precedence");
                options.currentSelectedIndex = -1;
                options.initOptionGroupItems();
            }
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

